package com.laiyima.zhongjiang.linghuilv.demo.home;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.laiyima.zhongjiang.linghuilv.demo.R;

import java.util.List;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class MyrealMachineActivity extends SwipeBackActivity implements View.OnClickListener {
    private LinearLayout linearLayout;
    private LinearLayout my_machine,bing_machine;
    private TextView my_machine_text,my_machine_lin,bing_machine_text,bing_machine_lin;
    private FragmentManager fragmentManager;



    protected boolean useThemestatusBarColor = false;//是否使用特殊的标题栏背景颜色，android5.0以上可以设置状态栏背景色，如果不使用则使用透明色值
    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                // getWindow().setStatusBarColor(getResources().getColor(R.color.colorTheme));
            } else {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myreal_machine);
        linearLayout= (LinearLayout) findViewById(R.id.mm_back);
        linearLayout.setOnClickListener(this);
        my_machine= (LinearLayout) findViewById(R.id.my_machine);
        my_machine.setOnClickListener(this);
        bing_machine= (LinearLayout) findViewById(R.id.bing_machine);
        bing_machine.setOnClickListener(this);
        setStatusBar();
        my_machine_text= (TextView) findViewById(R.id.my_machine_text);
        my_machine_lin= (TextView) findViewById(R.id.my_machine_lin);
        bing_machine_text= (TextView) findViewById(R.id.bing_machine_text);
        bing_machine_lin= (TextView) findViewById(R.id.bing_machine_lin);
        changeFragment(new MymachineFragment(), true);

    }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.mm_back:
                finish();
                break;
            case R.id.my_machine:
                my_machine_text.setTextColor(Color.parseColor("#2475E6"));
                my_machine_lin.setBackgroundColor(Color.parseColor("#2475E6"));
                bing_machine_text.setTextColor(Color.parseColor("#333333"));
                bing_machine_lin.setBackgroundColor(Color.parseColor("#FFFFFFFF"));
                changeFragment(new MymachineFragment(), true);
                break;
            case  R.id.bing_machine:
                bing_machine_text.setTextColor(Color.parseColor("#2475E6"));
                bing_machine_lin.setBackgroundColor(Color.parseColor("#2475E6"));
                my_machine_text.setTextColor(Color.parseColor("#333333"));
                my_machine_lin.setBackgroundColor(Color.parseColor("#FFFFFFFF"));
                changeFragment(new BingmachineFragment(), true);
                break;
        }

    }
    public void changeFragment(Fragment fragment, boolean isInit){
        //开启事物
        FragmentTransaction beginTransaction = getSupportFragmentManager().beginTransaction();
        beginTransaction.replace(R.id.machine_content, fragment);
        if (!isInit) {
            //加上该段代码Fragment就不会出现重影的效果
            beginTransaction.addToBackStack(null);
        }
        beginTransaction.commit();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getSupportFragmentManager().getFragments();
        if (getSupportFragmentManager().getFragments().size() > 0) {
            List<Fragment> fragments = getSupportFragmentManager().getFragments();
            for (Fragment mFragment : fragments) {
                mFragment.onActivityResult(requestCode, resultCode, data);
            }
        }
    }

}
